package com.common.core.utils.imageUtils;

import android.text.TextUtils;

import com.common.http.basecore.utils.LogInfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * Created by lajin on 16/9/9.
 */
public class FileUtils {

    /**
     * 获取图片大小,来源com.common.core.utils.FileUtils下getFileSize()方法
     * @param filePath
     * @return
     */
    private static long getFileSize(String filePath){
        FileChannel fc= null;
        try {
            File f= new File(filePath);
            if (f.exists() && f.isFile()){
                FileInputStream fis= new FileInputStream(f);
                fc= fis.getChannel();
                return fc.size();
            }else{
                LogInfo.log("file doesn't exist or is not a file");
            }
        } catch (FileNotFoundException e) {
            LogInfo.log(e.toString());
        } catch (IOException e) {
            LogInfo.log(e.toString());
        } finally {
            if (null!=fc){
                try{
                    fc.close();
                }catch(IOException e){
                    LogInfo.log(e.toString());
                }
            }
        }
        return 0;
    }

    public static boolean ImgInOfSize(String filePath, long maxSize){
        if (TextUtils.isEmpty(filePath)){
            return true;
        }

        long fileSize = getFileSize(filePath);
        if (fileSize < maxSize){
            return true;
        }

        return false;
    }
}
